package com.oromwin.repos.spike;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import com.oromwin.repos.common.ReposConfig;
import com.oromwin.repos.server.request.Request;
import com.oromwin.repos.server.response.Response;

import junit.framework.TestCase;

public class SocketTestCase extends TestCase {
    
    private final static String ROOTPATH = "";
    protected final static String INPUT = "";
    protected final static int COUNT = 0;
    
    protected static Socket _socket = null;
    protected static Request _req = null;
    protected static Response _res = null;

    public SocketTestCase(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        _socket = connect();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        disconnect(_socket);
    }

    protected Socket connect() {
        InetAddress addr = null;
        SocketAddress sockaddr = null;
        Socket socket = null;
        try {
            addr = InetAddress.getByName(ReposConfig.getInstance().getReposSocketServerIP());
            sockaddr = new InetSocketAddress(addr, ReposConfig.getInstance().getReposSocketPort());
            socket = new Socket();
            int timeoutMs = 3000;   // 3 seconds
            socket.connect(sockaddr, timeoutMs);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }

    protected void disconnect(Socket sock) {
        try {
            sock.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getROOTPATH() {
        return ROOTPATH;
    }
    
}
